var lockFlag = false;
var getSmscodeMin = 2;
var remainSecondTipTimer;
var remainCount;

$(function(){
	checkSafeLevel();
	$(".menu_list").find(".lion").removeClass("lion");
	//$(".menu_list").find("li:eq(7)").addClass("lion");
	$("#safecenter").addClass("lion");
	// 获取短信验证码
	$("#btn_sendSmCode").click(function() {
				// 验证是否达到了2分钟的时间
				if (!lockFlag) {
					if (!verifyInputField("phone,img_captcha_fixed"))
						return;
					if (!verifyInputLength("img_captcha_fixed", 4, 4))
						return;
					
					
					// 异步图形码验证
					$.get("captcha/check/pic/" + $("#img_captcha_fixed").val(), {}, function(data) {
								if (!data.result) {
									alertMsgAndFocus(data.message, "#img_captcha_fixed");
									return;
								}
								$(this).val("验证码发送中...").attr("disabled", true);
								// 异步获取短信验证码
								var rurl = "captcha/send/sm/editpwd/" +$("#msisdn").val();
								$.get(rurl, {}, function(data) {
											if (data.result) {
												lockFlag = true;
												remainCount = getSmscodeMin * 60;
												remainSecondTipTimer = setInterval(remainSencodTip, 1000);
												remainSencodTip();
												$("#hid_oprno").val("true");
											} else {
												lockFlag = false;
												$("#btn_sendSmCode").val("重新获取验证码").attr("disabled", false);
											}
											alertMsg(data.message);
										});
							});
				} else {
					alertMsg('"获取短信验证码"点击间隔不能少于' + getSmscodeMin + '分钟！');
				}
			});
			
			
			$("#nextStep").click(function() {
						if (!verifyInputField("img_captcha_fixed,sm_captcha_dynamic"))
							return;
						if (!verifyInputLength("img_captcha_fixed", 4, 4))
							return;
						if (!verifyInputLength("sm_captcha_dynamic", 6, 6))
							return;
						if (!$("#hid_oprno").val()) {
							alertMsg("请先获取短信验证码！");
							return;
						}
						// 图形码，短信码
						// 异步图形码验证
						$.get("captcha/check/pic/" + $("#img_captcha_fixed").val(), {}, function(data) {
								if (!data.result) {
									alertMsgAndFocus(data.message, "#img_captcha_fixed");
									return;
								}
								$.get("captcha/check/sm/4/" + $("#msisdn").val() + "/" + $("#sm_captcha_dynamic").val(), {}, function(data) {
												if (!data.result) {
													alertMsgAndFocus(data.message, "#sm_captcha_dynamic");
													return;
												}
												var userType = $("#nextStep").attr("userType");
												document.location.href=getRootUrl()+"usercenter/safecenter/validate_"+userType+"/2";
											});
							});

					});

});
//判断安全级别
function checkSafeLevel(){
	var msisdn_hidden = $("#msisdn_hidden").val();
	var email_hidden = $("#email_hidden").val();
	var safe = 0;
	if(msisdn_hidden){
		safe++;
	}
	if(email_hidden){
		safe++;
	}
	$("#safeLevel i").hide();
	$("#safeLevel i:eq("+safe+")").show();
}

//计算时间
function remainSencodTip() {
	if (remainCount == 0) {
		refreshImgKaptcha("kaptcha_fixed");
		$("#img_captcha_fixed").val('');
		$("#btn_sendSmCode").val("重新获取验证码").attr("disabled", false);
		lockFlag = false;
		clearInterval(remainSecondTipTimer);
		return;
	}
	$("#btn_sendSmCode").val(remainCount + "秒后可重新获取");
	remainCount--;
}

function refreshImgKaptcha(id) {
	$("#" + id)[0].src = "kaptcha.jpg?t=" + Math.random();
}
